﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Chapter3Applications.Models.PersistentModel
{
    [MetadataType(typeof(ProductAnnotations))]
    public partial class Product
    {
        // No field here
        
        //Custom Validation of duplicate entries
        public static ValidationResult ValidateNoDuplicateProductEntry(string ProductName)
        {
            ShoppingListModelContainer DBModel = new ShoppingListModelContainer();
            if (DBModel.ProductSet.Where(x => x.ProductName == ProductName).Any())
            {
                return new ValidationResult("This product already exists. Please enter another product.");
            }

            return ValidationResult.Success;
        }
    }

    internal sealed class ProductAnnotations
    {
        public int ProductId { get; set; }

        [Required(ErrorMessage = "Please enter a product name")]
        [CustomValidation(typeof(Product), "ValidateNoDuplicateProductEntry")]
        public string ProductName { get; set; }

        
    }
}